<?php

class MY_Calendar extends CI_Calendar {

    function My_Calendar($config = array())
    {
        parent::CI_Calendar($config);
    }
	
	function advanced_generate($year = '', $month = '', $data = array())
	{
		// Set and validate the supplied month/year
		if ($year == '')
			$year  = date("Y", $this->local_time);
			
		if ($month == '')
			$month = date("m", $this->local_time);
			
 		if (strlen($year) == 1)
			$year = '200'.$year;
		
 		if (strlen($year) == 2)
			$year = '20'.$year;

 		if (strlen($month) == 1)
			$month = '0'.$month;
		
		$adjusted_date = $this->adjust_date($month, $year);
		
		$month	= $adjusted_date['month'];
		$year	= $adjusted_date['year'];
		
		// Determine the total days in the month
		$total_days = $this->get_total_days($month, $year);
						
		// Set the starting day of the week
		$start_days	= array('sunday' => 0, 'monday' => 1, 'tuesday' => 2, 'wednesday' => 3, 'thursday' => 4, 'friday' => 5, 'saturday' => 6);
		$start_day = ( ! isset($start_days[$this->start_day])) ? 0 : $start_days[$this->start_day];
		
		// Set the starting day number
		$local_date = mktime(12, 0, 0, $month, 1, $year);
		$date = getdate($local_date);
		$day  = $start_day + 1 - $date["wday"];
		
		while ($day > 1)
		{
			$day -= 7;
		}
		
		// Set the current month/year/day
		// We use this to determine the "today" date
		$cur_year	= date("Y", $this->local_time);
		$cur_month	= date("m", $this->local_time);
		$cur_day	= date("j", $this->local_time);
		
		$is_current_month = ($cur_year == $year AND $cur_month == $month) ? TRUE : FALSE;
	
		// Generate the template data array
		$this->parse_template();
	
		// Begin building the calendar output						
		$out = $this->temp['table_open'];
		$out .= "\n";	

		$out .= "\n";		
		$out .= $this->temp['heading_row_start'];
		$out .= "\n";
		
		// "previous" month link
		if ($this->show_next_prev == TRUE)
		{
			// Add a trailing slash to the  URL if needed
			$this->next_prev_url = preg_replace("/(.+?)\/*$/", "\\1/",  $this->next_prev_url);
		
			$adjusted_date = $this->adjust_date($month - 1, $year);
			$out .= str_replace('{previous_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_previous_cell']);
			$out .= "\n";
		}

		// Heading containing the month/year
		$colspan = ($this->show_next_prev == TRUE) ? 5 : 7;
		
		$this->temp['heading_title_cell'] = str_replace('{colspan}', $colspan, $this->temp['heading_title_cell']);
		$this->temp['heading_title_cell'] = str_replace('{heading}', $this->get_month_name($month)."&nbsp;".$year, $this->temp['heading_title_cell']);
		
		$out .= $this->temp['heading_title_cell'];
		$out .= "\n";

		// "next" month link
		if ($this->show_next_prev == TRUE)
		{		
			$adjusted_date = $this->adjust_date($month + 1, $year);
			$out .= str_replace('{next_url}', $this->next_prev_url.$adjusted_date['year'].'/'.$adjusted_date['month'], $this->temp['heading_next_cell']);
		}

		$out .= "\n";		
		$out .= $this->temp['heading_row_end'];
		$out .= "\n";

		// Write the cells containing the days of the week
		$out .= "\n";	
		$out .= $this->temp['week_row_start'];
		$out .= "\n";

		$day_names = $this->get_day_names();

		for ($i = 0; $i < 7; $i ++)
		{
			$out .= str_replace('{week_day}', $day_names[($start_day + $i) %7], $this->temp['week_day_cell']);
		}

		$out .= "\n";
		$out .= $this->temp['week_row_end'];
		$out .= "\n";

		// Build the main body of the calendar
		while ($day <= $total_days)
		{
			$out .= "\n";
			$out .= $this->temp['cal_row_start'];
			$out .= "\n";

			for ($i = 0; $i < 7; $i++)
			{
				$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_start_today'] : $this->temp['cal_cell_start'];
			
				if ($day > 0 AND $day <= $total_days)
				{ 					
					if (isset($data[$day]))
					{	
					
						$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
						$out .= str_replace('{day}', $day, $temp);
					
						if (is_array($data[$day]))
						{
							$s = 0;
							foreach ($data[$day] as $key => $value)
							{
								if ($key > $s)
								{
									$s = $key;
								}
							}
							
							for ($t = 0; $t < ($s + 1); $t++)
							{
								if (isset($data[$day][$t]))
								{
									$out .= '<a href="' . site_url("tours/edit/" . $data[$day][$t]['id']) . '" title="'. $data[$day][$t]['tour_name'] .'" class="calItem';
									
									if (isset($data[$day][$t]['first_day']))
									{
										$out .= ' calStart';
									}
									
									if (isset($data[$day][$t]['last_day']))
									{
										$out .= ' calEnd';
									}
									
									$out .= '" style="background-color:' . $data[$day][$t]['color'] . '">';
									
									if (isset($data[$day][$t]['first_day']))
									{
										//$out .= substr($data[$day][$t]['tour_name'], 0, 8) . '...';
										$out .= $data[$day][$t]['tour_code'];
									}
									
									$out .= '</a>';
								} else
								{
									$out .= '<a class="calEmptyCell"></a>';
								}
							}
						}
						
						
					}
					else
					{
						// Cells with no content
						$temp = ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_no_content_today'] : $this->temp['cal_cell_no_content'];
						$out .= str_replace('{day}', $day, $temp);
					}
				}
				else
				{
					// Blank cells
					$out .= $this->temp['cal_cell_blank'];
				}
				
				$out .= ($is_current_month == TRUE AND $day == $cur_day) ? $this->temp['cal_cell_end_today'] : $this->temp['cal_cell_end'];					  	
				$day++;
			}
			
			$out .= "\n";		
			$out .= $this->temp['cal_row_end'];
			$out .= "\n";		
		}

		$out .= "\n";		
		$out .= $this->temp['table_close'];

		return $out;
	}
	
	function calendar_array($data, $month, $year)
	{
		$length = cal_days_in_month(CAL_GREGORIAN, $month, $year);
		
		//Prepare array
		$ar = array();
		
		for ($i = 1; $i <= $length; $i++)
		{
			$ar[$i] = array();
		}
		
		// Now go through the data
		for ($i = 0; $i < count($data); $i++)
		{
			// Get the day
			$d = extract_day_from_db($data[$i]['departure']);
			
			$c = 0;
			
			
			while (isset($ar[$d][$c]))
			{
				$c++;
			}
			
			$ar[$d][$c] = array(
								'id' => $data[$i]['id'],
								'tour_name' => $data[$i]['tour_name'],
								'tour_code' => $data[$i]['tour_code'],
								'first_day' => 1,
								'color' => $data[$i]['calendar_color']
								);
			
			// Extra days
			for ($n = 1; $n <= $data[$i]['tour_nights'] && ($d + $n) <= $length; $n++)
			{
				$ar[$d + $n][$c] = array(
								'id' => $data[$i]['id'],
								'tour_name' => $data[$i]['tour_name'],
								'tour_code' => $data[$i]['tour_code'],
								'color' => $data[$i]['calendar_color']
								);
				
				if ($n == $data[$i]['tour_nights'])
				{
					$ar[$d + $n][$c]['last_day'] = 1;
				}
			}
			
			/*
			[id] => 13
                    [tourtype_id] => 3
                    [departure] => 2009-06-29 00:00:00
                    [capacity] => 29
                    [passengers] => 0
                    [use_default_price] => 1
                    [custom_price] => 0
                    [special_offer] => 0
                    [active] => 1
                    [created_at] => 2009-06-12 18:11:12
                    [tour_name] => 5 Day Skye and Highland Fling
                    [tour_code] => 5DHF
                    [tour_nights] => 4 */
			
			
			
		}
		
		return $ar;
	}
	
	
	
}

?>